package cn.edu.cug.cs.gtl.gsm.aof.ftp;

import cn.edu.cug.cs.gtl.common.Identifier;
import cn.edu.cug.cs.gtl.gsm.aof.DataFiles;
import cn.edu.cug.cs.gtl.gsm.item.DataItem;
import cn.edu.cug.cs.gtl.gsm.item.IndexMap;
import cn.edu.cug.cs.gtl.gsm.item.MetaItem;
import org.apache.commons.net.ftp.FTPClient;

import java.io.*;

public class FTPDataFiles implements DataFiles {
    private String fileName = "ftp_sm_data";
    private FTPClient ftpClient;
    private String workDirectory;
    private MetaItem metaItem;
    private IndexMap pageMap;

    public FTPDataFiles(FTPClient ftpClient, String workDirectory, MetaItem metaItem, IndexMap pageMap) {
        this.ftpClient = ftpClient;
        this.workDirectory = workDirectory;
        this.metaItem = metaItem;
        this.pageMap = pageMap;
        fileName = "ftp_sm_data_"+ this.metaItem.getBaseName()+ "_0";
    }

    @Override
    public void setCurrentFileOrder(int order) {
        fileName = "ftp_sm_data_" + this.metaItem.getBaseName()+ "_" + String.valueOf(order);
    }

    @Override
    public boolean create() throws IOException {
        //在FTP上的workDirectory+baseName目录下创建文件fileName
        String fullPathName = FTPStorageManager.concatenate(workDirectory,metaItem.getBaseName(),fileName);
        OutputStream outputStream = ftpClient.storeFileStream(fullPathName);
        outputStream.close();
        ftpClient.completePendingCommand();
        return true;
    }

    @Override
    public boolean drop() throws IOException {
        //在FTP上的workDirectory+baseName目录下删除文件fileName
        String fullPathName = FTPStorageManager.concatenate(workDirectory,metaItem.getBaseName(),fileName);
        return ftpClient.deleteFile(fullPathName);
    }

    @Override
    public boolean exists() throws IOException {
        //在FTP上的workDirectory+baseName目录下判断文件fileName是否存在
        String fullPathName = FTPStorageManager.concatenate(workDirectory,metaItem.getBaseName(),fileName);
        return FTPStorageManager.existsFile(ftpClient,fullPathName);
    }

    @Override
    public boolean write(DataItem dataItem) throws IOException {
        //在FTP上的workDirectory+baseName目录下打开文件fileName，并写入dataItem
        int order = metaItem.getCollectionOrder(dataItem.getPageIdentifier());
        setCurrentFileOrder(order);
        String fullPathName = FTPStorageManager.concatenate(workDirectory,metaItem.getBaseName(),fileName);
        OutputStream outputStream = ftpClient.appendFileStream(fullPathName);
        DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
        dataOutputStream.write(dataItem.getPageContent());
        outputStream.close();
        dataOutputStream.close();
        ftpClient.completePendingCommand();
        return true;
    }

    @Override
    public DataItem read(Identifier pageIdentifier) throws IOException {
        //在FTP上的workDirectory+baseName目录下打开文件fileName，并读取dataItem
        int order = metaItem.getCollectionOrder(pageIdentifier);
        setCurrentFileOrder(order);
        long pos = pageIdentifier.longValue()-metaItem.getCollectionSize()* order;
        pos = pos * metaItem.getPageSize();
        String fullPathName = FTPStorageManager.concatenate(workDirectory,metaItem.getBaseName(),fileName);
        ftpClient.setRestartOffset(0);
        InputStream inputStream = ftpClient.retrieveFileStream(fullPathName);
        DataInputStream dataInputStream = new DataInputStream(inputStream);
        byte [] bs = new byte[metaItem.getPageSize()];
        dataInputStream.skipBytes((int)pos);
        dataInputStream.readFully(bs);
        inputStream.close();
        dataInputStream.close();
        this.ftpClient.completePendingCommand();
        return new DataItem(pageIdentifier,bs);
    }
}
